package com.grandtech.insurance.common.core.bean;

import com.grandtech.insurance.common.db.mongdb.factory.MongoFactory;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.io.Serializable;
import java.util.Map;

@XStreamAlias("SimpleScheme")
public class TileSchemeCore implements Serializable {

    @XStreamAlias("clientURI")
    private String clientURI;

    @XStreamAlias("tileId")
    private String tileId;

    @XStreamAlias("minzoom")
    private Integer minZoom;

    @XStreamAlias("maxzoom")
    private Integer maxZoom;

    @XStreamAlias("autoScale")
    private Boolean autoScale = false;

    @XStreamAlias("extent")
    private Integer extent = 4096;

    @XStreamAlias("buffer")
    private Integer clipBuffer;

    @XStreamAlias("wkid")
    private String wkid;

    @XStreamAlias("tableName")
    private String tableName;

    @XStreamAlias("attributes")
    private Map attrFields;

    @XStreamAlias("shapeField")
    private String shapeField = "shape";

    @XStreamAlias("sqlTemplate")
    private String sqlTemplate;

    @XStreamAlias("beforeTile")
    private String beforeTile;

    /**
     * 是否切标注
     */
    @XStreamAlias("tileSymbol")
    private Boolean tileSymbol = false;

    @XStreamAlias("symbolTileId")
    private String symbolTileId;

    public String getClientURI() {
        return clientURI;
    }

    public void setClientURI(String clientURI) {
        this.clientURI = clientURI;
    }

    public String getTileId() {
        return tileId;
    }

    public void setTileId(String tileId) {
        this.tileId = tileId;
    }

    public Integer getMinZoom() {
        return minZoom;
    }

    public void setMinZoom(Integer minZoom) {
        this.minZoom = minZoom;
    }

    public Integer getMaxZoom() {
        return maxZoom;
    }

    public void setMaxZoom(Integer maxZoom) {
        this.maxZoom = maxZoom;
    }

    public Boolean getAutoScale() {
        return autoScale;
    }

    public void setAutoScale(Boolean autoScale) {
        this.autoScale = autoScale;
    }

    public Integer getExtent() {
        return extent;
    }

    public void setExtent(Integer extent) {
        this.extent = extent;
    }

    public Integer getClipBuffer() {
        return clipBuffer;
    }

    public void setClipBuffer(Integer clipBuffer) {
        this.clipBuffer = clipBuffer;
    }

    public String getWkid() {
        return wkid;
    }

    public void setWkid(String wkid) {
        this.wkid = wkid;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public Map getAttrFields() {
        return attrFields;
    }

    public void setAttrFields(Map attrFields) {
        this.attrFields = attrFields;
    }

    public String getShapeField() {
        return shapeField;
    }

    public void setShapeField(String shapeField) {
        this.shapeField = shapeField;
    }

    public String getSqlTemplate() {
        if (sqlTemplate != null) return sqlTemplate;
        return getTileSql();
    }

    public void setSqlTemplate(String sqlTemplate) {
        this.sqlTemplate = sqlTemplate;
    }

    /**
     * 获取切片SQL
     *
     * @return
     */
    private String getTileSql() {
        if (shapeField == null) return null;
        StringBuilder sqlBuilder = new StringBuilder("select ");
        for (Object field : attrFields.keySet()) {
            sqlBuilder.append(field).append(" ,");
        }
        sqlBuilder.append("st_astext(").append(shapeField).append(") as ").append(shapeField).append(" from ").append(tableName)
                .append(" where st_intersects(").append(shapeField).append(",st_geomfromtext('?',").append(wkid).append("))");
        return sqlBuilder.toString();
    }

    public String getBeforeTile() {
        return beforeTile;
    }

    public void setBeforeTile(String beforeTile) {
        this.beforeTile = beforeTile;
    }

    public Boolean getTileSymbol() {
        if (tileSymbol == null) return false;
        return tileSymbol;
    }

    public void setTileSymbol(Boolean tileSymbol) {
        this.tileSymbol = tileSymbol;
    }

    public String getSymbolTileId() {
        return symbolTileId;
    }

    public void setSymbolTileId(String symbolTileId) {
        this.symbolTileId = symbolTileId;
    }

    public String getMongoTileName() {
        return "tiles_dt_ytdk0305";
    }


    public MongoTemplate getMongoTemplate() {
        if (clientURI == null) return null;
        return MongoFactory.getSimpleMongoDbFactory(clientURI);
    }
}
